/*
 * This computer program is the confidential information and proprietary trade
 * secret of Apptonelabs, Inc. Possessions and use of this program must
 * conform strictly to the license agreement between the user and
 * Apptonelabs, Inc., and receipt or possession does not convey any rights
 * to divulge, reproduce, or allow others to use this program without specific
 * written authorization of Apptonelabs, Inc.
 * 
 * Copyright (c) 2012-2015 Apptonelabs, Inc. All Rights Reserved.
 */
package com.apptonelabs.vnc.vphone;

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

class ClipboardFrame extends Frame implements WindowListener, ActionListener {

	TextArea textArea;
	Button clearButton, closeButton;
	String selection;
	VPhone viewer;

	//
	// Constructor.
	//

	ClipboardFrame(VPhone v) {
		super("TightVNC Clipboard");

		viewer = v;

		GridBagLayout gridbag = new GridBagLayout();
		setLayout(gridbag);

		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weighty = 1.0;

		textArea = new TextArea(5, 40);
		gridbag.setConstraints(textArea, gbc);
		add(textArea);

		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 1.0;
		gbc.weighty = 0.0;
		gbc.gridwidth = 1;

		clearButton = new Button("Clear");
		gridbag.setConstraints(clearButton, gbc);
		add(clearButton);
		clearButton.addActionListener(this);

		closeButton = new Button("Close");
		gridbag.setConstraints(closeButton, gbc);
		add(closeButton);
		closeButton.addActionListener(this);

		pack();

		addWindowListener(this);
	}

	//
	// Set the cut text from the RFB server.
	//

	void setCutText(String text) {
		selection = text;
		textArea.setText(text);
		if (isVisible()) {
			textArea.selectAll();
		}
	}

	//
	// When the focus leaves the window, see if we have new cut text and
	// if so send it to the RFB server.
	//

	public void windowDeactivated(WindowEvent evt) {
		if (selection != null && !selection.equals(textArea.getText())) {
			selection = textArea.getText();
			viewer.setCutText(selection);
		}
	}

	//
	// Close our window properly.
	//

	public void windowClosing(WindowEvent evt) {
		setVisible(false);
	}

	//
	// Ignore window events we're not interested in.
	//

	public void windowActivated(WindowEvent evt) {
	}

	public void windowOpened(WindowEvent evt) {
	}

	public void windowClosed(WindowEvent evt) {
	}

	public void windowIconified(WindowEvent evt) {
	}

	public void windowDeiconified(WindowEvent evt) {
	}

	//
	// Respond to button presses
	//

	public void actionPerformed(ActionEvent evt) {
		if (evt.getSource() == clearButton) {
			textArea.setText("");
		} else if (evt.getSource() == closeButton) {
			setVisible(false);
		}
	}
}
